#!/usr/bin/env python
# coding:utf-8
# @author heitfei

'''
tcp time server using socket
只能维持和处理一个请求，处理完一个请求之后再处理下一个请求
'''

import socket
import time

HOST = ''
PORT = 23567
BUFFSIZE = 1024
ADDR = (HOST, PORT)

tcpSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)
tcpSocket.bind(ADDR)
tcpSocket.listen(5)

while True:
    print "waiting for connection ..."
    clientSocket, addr = tcpSocket.accept()
    print "...connected from:",addr

    while True:
        '''在此服务一个链接，直到该链接断卡，或者发来空数据
        '''
        data = clientSocket.recv(BUFFSIZE)
        if not data:
            break
        clientSocket.send('[%s] %s' % (time.ctime(), data))

tcpSocket.close()